﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

// FROM ADO.Net
using System.Data.SqlClient;
using System.Data;

namespace yoADO
{
    public partial class Asynchronous : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            String connectionString = "Data Source=localhost;Initial Catalog=Northwind;Persist Security Info=True;User ID=yoADO;Password=cs490;Asynchronous Processing=true";
            //String connectionString = "Data Source=web.ankitbuti.com;Initial Catalog=Northwind;Persist Security Info=True;User ID=yoADO;Password=cs490;Asynchronous Processing=true";

            String queryString = "WAITFOR DELAY '00:00:03'; SELECT [OrderID], [EmployeeID], [OrderDate], [CustomerID], [RequiredDate], [ShippedDate], [ShipAddress] FROM [Orders]";

            RunCommandAsynchronously(queryString, connectionString, this);
          
            
        }

        private static void RunCommandAsynchronously(string commandText, string connectionString, Asynchronous obj)
        {
            // Given command text and connection string, asynchronously execute
            // the specified command against the connection. For this example,
            // the code displays an indicator as it is working, verifying the 
            // asynchronous behavior. 
            using (SqlConnection connection = new SqlConnection(connectionString))
            {
                try
                {
                    SqlCommand command = new SqlCommand(commandText, connection);

                    connection.Open();
                    IAsyncResult result = command.BeginExecuteReader();

                    // Although it is not necessary, the following code
                    // displays a counter in the console window, indicating that 
                    // the main thread is not blocked while awaiting the command 
                    // results.
                    int count = 0;
                    obj.Response.Write("<b>");
                    while (!result.IsCompleted)
                    {
                        count += 1;
                        
                        obj.Response.Write("Wait " + count.ToString() + ", ");
                        // Wait for 1/20 second, so the counter
                        // does not consume all available resources 
                        // on the main thread.
                        System.Threading.Thread.Sleep(1);
                    }
                    obj.Response.Write("</b>");

                    using (SqlDataReader reader = command.EndExecuteReader(result))
                    {
                        DisplayResults(reader, obj);
                    }
                }
                catch (SqlException ex)
                {
                    Console.WriteLine("Error ({0}): {1}", ex.Number, ex.Message);
                }
                catch (InvalidOperationException ex)
                {
                    Console.WriteLine("Error: {0}", ex.Message);
                }
                catch (Exception ex)
                {
                    // You might want to pass these errors
                    // back out to the caller.
                    Console.WriteLine("Error: {0}", ex.Message);
                }
            }
        }


        private static void DisplayResults(SqlDataReader reader, Asynchronous obj)
        {

            System.Text.StringBuilder sb = new System.Text.StringBuilder();
            sb.Append("<table border=\"1\">");
            sb.Append("<tr><th>OrderID</th><th>EmployeeID</th><th>OrderDate</th><th>CustomerID</th><th>RequiredDate</th><th>ShippedDate</th><th>ShipAddress</th></tr>");
            
            // Display the data within the reader.
            while (reader.Read())
            {
                sb.Append("<tr>");
                sb.Append("<td>" + reader["OrderID"].ToString() + "</td>");
                sb.Append("<td>" + reader["EmployeeID"].ToString() + "</td>");
                sb.Append("<td>" + reader["OrderDate"].ToString() + "</td>");
                sb.Append("<td>" + reader["CustomerID"].ToString() + "</td>");
                sb.Append("<td>" + reader["RequiredDate"].ToString() + "</td>");
                sb.Append("<td>" + reader["ShippedDate"].ToString() + "</td>");
                sb.Append("<td>" + reader["ShipAddress"].ToString() + "</td>");
                sb.Append("</tr>");
            }
            sb.Append("</table>");
            obj.lbl1.Text = sb.ToString();
        }

    }
}